package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("user/shoppingCart")
@Api("/购物车相关接口")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * @Description : 向购物车中添加商品
     *  * @param shoppingCartDTO
     * @Return : com.sky.result.Result
     */
    @PostMapping("add")
    @ApiOperation("向购物车中添加商品")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("向购物车中添加商品");
        shoppingCartService.add(shoppingCartDTO);
        return Result.success();
    }

    /**
     * @Description : 显示购物车列表
     *  * @param id
     * @Return : com.sky.result.Result
     */
    @GetMapping("list")
    @ApiOperation("显示购物车列表")
    public Result<List<ShoppingCart>> list(Long id){
        log.info("显示购物车列表");
        List<ShoppingCart> list = shoppingCartService.showShoppingCart(id);
        return Result.success(list);
    }

    /**
     * @Description : 清空购物车
     *  * @param
     * @Return : com.sky.result.Result
     */
    @DeleteMapping("clean")
    @ApiOperation("清空购物车")
    public Result clean(){

        log.info("清空购物车");
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }

    /**
     * @Description : 减少购物车中商品数量
     *  * @param
     * @Return : com.sky.result.Result
     */
    @PostMapping("sub")
    @ApiOperation("减少购物车中商品数量")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("减少购物车中商品数量");
        shoppingCartService.subNumber(shoppingCartDTO);
        return Result.success();
    }
}
